/**
 * @class tuan.HomeController
 * @extends extendsClass
 * Description
 */

 Ext.define('tuan.controller.HomeController', {
  extend: 'Ext.app.Controller',
  refs : [
    {ref : "navi", selector : "home #navigator_zone"},
    {ref : 'menu', selector : "home navi_panel"},
    {ref : 'wapper',selector : 'donhang_wrapper_panel'},
  ],
  views : ["home.HomeScreen","tree.MenuPanel","sanpham.SanphamWrapperView","donhang.DonHangWapperView",
  'donhang.AddNewDonHangWindow','donhang.AddNewDonHangWindowStep2'],
  stores : ["MenuStore"],
  loadDashboard : false,
  init : function (){

   this.control({

     "#logoutbutton" : {
       click : function(){
         sessionStorage.removeItem("sessionUser");
         window.location.reload();
	    		// this.application.viewport.getLayout().setActiveItem(0);
        }
      },
      '#homebtnAddOrder' : {
          click : function(){
              window_add_new_order = Ext.create('tuan.view.donhang.AddNewDonHangWindow');
              window_add_new_order.show();
              Ext.getCmp("choose_product_oredr").add({xtype : "products_widget_list_view"});
          }
      },
      "home" : {
        beforerender : this.loadnavi
      },
      "#navi_panel_id" : {
         itemclick : this.loaditemmenu
      },
    })
 },
 loadnavi  : function(){
  Ext.getCmp("center_panel").getLayout().setActiveItem(3);
  this.application.runaction('DashboardController','Index');
  this.loadDashboard = true;
  var nav = this.getNavi();
  nav.add({xtype : "navi_panel"})

  },
  loaditemmenu : function(view,rec,item,index,eventObj){
     var id = rec.get('id');
     console.log(id);
     if(id==='products'){
        Ext.getCmp("center_panel").getLayout().setActiveItem(1);
        this.application.runaction('ProductController','Index');
     }else if(id === 'customers'){
        level = sessionStorage.getItem('level');
        if( level>=1 && level <=3 ){
          Ext.getCmp("center_panel").getLayout().setActiveItem(0);
        }else{
          Ext.Msg.alert("Không có quyền","Xin lỗi bạn không có quyền truy cập");
        }
     }else if(id === 'orders'){
        // Ext.getCmp("center_panel").getLayout().setActiveItem(2).add({xtype : 'donhang_wrapper_panel'});
        Ext.getCmp("center_panel").getLayout().setActiveItem(2);
        this.application.runaction('DonHangController','Index');
     }else if(id === 'home'){
        
          Ext.getCmp("center_panel").getLayout().setActiveItem(3);
          if(this.loadDashboard === false){
              this.application.runaction('DashboardController','Index');
          }


     }else if(id === 'adduser'){
        Ext.getCmp("center_panel").getLayout().setActiveItem(4);
        // if(this.loadDashboard === false){
        //     this.application.runaction('DashboardController','Index');
        // }
     }else if(id === 'phanquyen'){
        Ext.getCmp("center_panel").getLayout().setActiveItem(5);
        // if(this.loadDashboard === false){
        //     this.application.runaction('DashboardController','Index');
        // }
     }
  }
});